package com.example.hellondk;

import com.example.constant.Constant;
import com.example.customerview.IDCardSaveDialog;
import com.example.fragment.SmartCardActivity;
import com.example.util.IDCardDecodeUtil;
import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.markupartist.android.widget.ActionBar.IntentAction;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class IDCardReadActivity extends Activity {
	public static final String TAG = "com.example.hellondk.IDCardReadActivity";
	private EditText nameEdit;
	private EditText sexEdit;
	private EditText nationEdit;
	private EditText birthdayEdit;
	private EditText addressEdit;
	private EditText idCodeEdit;
	
	public native String helloNdk();
	public native String getName();
	public String idcardInfo;
	
	static{
		System.loadLibrary("Hello");
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.identity);
		initWidet();
		//idcardInfo = Constant.str3;
		idcardInfo = helloNdk();
	}

	public void initWidet(){
		nameEdit = (EditText) findViewById(R.id.nameEdit);
		sexEdit = (EditText) findViewById(R.id.sexEdit);
		nationEdit = (EditText) findViewById(R.id.nationEdit);
		birthdayEdit = (EditText) findViewById(R.id.birthdayEdit);
		addressEdit = (EditText) findViewById(R.id.addressEdit);
		idCodeEdit = (EditText) findViewById(R.id.idCodeEdit);
		
		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar_identity);
		IntentAction homeAction = new IntentAction(this, createIntent(this), R.drawable.back);
		actionBar.setHomeAction(homeAction, new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(IDCardReadActivity.this, SmartCardActivity.class);
				startActivity(intent);
			}
		});
		final Action saveAction = new IntentAction(this, createSaveIntent(this), R.drawable.ic_title_share_default);
        actionBar.addAction(saveAction);
	}
	
	public static Intent createSaveIntent(Context context){
		Intent i = new Intent(context, IDCardSaveDialog.class);
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		return i;
	}
	
	public static Intent createIntent(Context context) {
		Intent i = new Intent(context, ActionbarActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		return i;
	}
	
	public void diaplayIDCardValues(){
		nameEdit.setText(IDCardDecodeUtil.decodeName(idcardInfo));
		sexEdit.setText(IDCardDecodeUtil.decodeSex(idcardInfo));
		nationEdit.setText(IDCardDecodeUtil.decodeNation(idcardInfo));
		birthdayEdit.setText(IDCardDecodeUtil.decodeBirthday(idcardInfo));
		addressEdit.setText(IDCardDecodeUtil.decodeAddress(idcardInfo));
		idCodeEdit.setText(IDCardDecodeUtil.decodeIDCardCode(idcardInfo));
	}
	
	public void resetIDCardValues(){
		nameEdit.setText("");
		sexEdit.setText("");
		nationEdit.setText("");
		birthdayEdit.setText("");
		addressEdit.setText("");
		idCodeEdit.setText("");
	}
	
	public void click(View view){
		switch (view.getId()) {
		case R.id.get:
		{
			diaplayIDCardValues();
			Log.i(TAG, IDCardDecodeUtil.decodeIDCardCode(Constant.str1));
			break;
		}
		case R.id.reset:
		{
			resetIDCardValues();
			break;
		}

		default:
			break;
		}
	}
	
}
